<%@ include file="database_ini.jsp"%>


<%
	String dropid = request.getParameter("DROPID");
	request.setAttribute("DROPID",dropid);
	response.sendRedirect("index.jsp?DROPID=" +dropid);
	/*********debug*/
	System.out.println("DROPID: " + dropid);
	/**********/

	String Loc[] = request.getParameterValues("Loc");
	if (Loc != null) {
		for (int i = 0; i < Loc.length; i++) {
			out.println(Loc[i]);
			System.out.println("Loc: " + Loc[i]);
		}
	}
	
	/*********debug*/
			System.out.println("Loc: " + Loc);
			/**********/
	
	String Sku[] = request.getParameterValues("Sku");
	if (Sku != null) {
		for (int i = 0; i < Sku.length; i++) {
			out.println(Sku[i]);
			System.out.println("Sku: " + Sku[i]);
		}
	}
	String CountQty_str[] = request.getParameterValues("CountQty");
	int CountQty_int[] = new int[CountQty_str.length];
	if (CountQty_str != null) {
		for (int i = 0; i < CountQty_str.length; i++) {
			
			out.println(CountQty_str[i]);
			CountQty_int[i] = Integer.parseInt(CountQty_str[i]);
			/*********debug*/
			System.out.println("CountQty: " + CountQty_str[i]);
			System.out.println("CountQty_str.length: "
					+ CountQty_str.length);
			System.out.println("CountQty_int: " + CountQty_int.length);
			/**********/
		}
	}
	
	System.out.println("CountQty_int: " + CountQty_int);
	
	for (int a = 0; a < Loc.length; a++) {

		int CountQty_int_re = CountQty_int[a];
		int CountQty_int_re_temp = 0;
		int Qty_rs = 0;
		String Lot_rs ="";
		String ID_rs ="";
		Statement st = null;
		String sql = "select CCKEY, CCDROPID, Loc, Sku, Descr, UPCCode, Lottable05, Casecnt, DisplaySystemCount, Lot, ID, Qty from CCDET "
				+ "WHERE Status = 0 and CCDROPID = ? and Sku = ? and Loc = ? order by CCKEY, CCDROPID, Loc, Sku, Descr, UPCCode, Lottable05, Casecnt, DisplaySystemCount, Lot DESC, ID DESC";

		pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, dropid);
		pstmt.setString(2, Sku[a]);
		pstmt.setString(3, Loc[a]);

		
		ResultSet rs = pstmt.executeQuery();
		boolean firstrecord = true;
		
		String CCKEY =null;
		while (rs.next() && CountQty_int_re!=0) {
			CCKEY = rs.getString("CCKEY");
			//String Loc_rs = rs.getString("Loc");
			//String Sku_rs = rs.getString("Sku");
			
			if(!firstrecord){
			
				if(CountQty_int_re > Qty_rs) {
					CountQty_int_re_temp = Qty_rs;
					CountQty_int_re = CountQty_int_re - Qty_rs;
				}
				
				st = conn.createStatement();
				st.executeUpdate("update CCDET set CountQty="
						+ CountQty_int_re_temp + " where Sku='" + Sku[a]
						+ "' and Loc = '" + Loc[a] + "' and Lot = '"
						+ Lot_rs + "' and ID = '" + ID_rs + "'");
			}
			firstrecord = false;
			
			
			
			Lot_rs = rs.getString("Lot");
			ID_rs = rs.getString("ID");
			Qty_rs = rs.getInt("Qty");
			
			/*********debug*/
			System.out.println("***************Round " + a);
			System.out.println("Loc_rs: " + Loc[a]);
			System.out.println("Sku_rs: " + Sku[a]);
			System.out.println("Lot_rs: " + Lot_rs);
			System.out.println("ID_rs: " + ID_rs);
			System.out.println("Qty_rs: " + Qty_rs);
			/**********/
			
			
			if (CountQty_int_re <= Qty_rs) {
				
				/*********debug*/			
				System.out.println("CountQty_int_re_temp: " + CountQty_int_re_temp);
				System.out.println("CountQty_int_re: " + CountQty_int_re);
				/**********/
				CountQty_int_re_temp = CountQty_int_re;
				CountQty_int_re = 0;
			}
			
			

			/*********debug*/			
			System.out.println("CountQty_int_re_temp: " + CountQty_int_re_temp);
			System.out.println("CountQty_int_re: " + CountQty_int_re);
			/**********/
			
		}
		
		if (CountQty_int_re > Qty_rs) {
				CountQty_int_re_temp = CountQty_int_re;
				CountQty_int_re = 0;		
		}
			
			st = conn.createStatement();
			st.executeUpdate("update CCDET set CountQty="
					+ CountQty_int_re_temp + ", Status = 2 where Sku='" + Sku[a]
					+ "' and Loc = '" + Loc[a] + "' and Lot = '"
					+ Lot_rs + "' and ID = '" + ID_rs + "'");


			Statement st2 = conn.createStatement();
			
			st2.executeUpdate("update CCHDR set Status = 2 where CCKEY ='" + CCKEY + "' and NOT EXISTS (select * from CCDET where CCKEY = '" + CCKEY + "' and Status <> '2' )");
	}
	
	
	conn.close();
%>